package com.sm.imlibrary.presenter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sm.imlibrary.roondao.RoomDaoApi;
import com.sm.imlibrary.event.MessageEvent;
import com.sm.imlibrary.http.HttpIMCallback;
import com.sm.imlibrary.http.IMHttpUtil;
import com.sm.imlibrary.imsdk.SIMConversation;
import com.sm.imlibrary.imsdk.SIMConversationType;
import com.sm.imlibrary.imsdk.SIMManager;
import com.sm.imlibrary.imsdk.SIMMessage;
import com.sm.imlibrary.model.Message;
import com.sm.imlibrary.model.SIMElemType;
import com.sm.imlibrary.model.TextMessage;
import com.sm.imlibrary.model.http.MessageHttpBean;
import com.sm.imlibrary.viewfeatures.ChatView;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 聊天界面逻辑
 * created by zhud on 2019/7/9
 */
public class ChatPresenter implements Observer {
    private SIMConversation simConversation;
    private ChatView presenter;
    private String identify;

    public ChatPresenter(ChatView presenter, String identify, SIMConversationType simConversationType) {
        this.presenter = presenter;
        this.identify = identify;
        MessageEvent.getInstance().addObserver(this);
        simConversation = SIMManager.getInstance().getConversation(simConversationType, identify);
    }

    /**
     * 发送消息
     *
     * @param messages 发送的消息
     */
    public void sendMessage(List<Message> messages) {
        JSONArray array = new JSONArray();
        for (Message message : messages) {
            JSONObject json = new JSONObject();
            if (message.getMessage().getType() == SIMElemType.Text) {
                TextMessage textMessage = (TextMessage) message;
                json.put("to", identify);
                json.put("content", textMessage.getContent());
            }
            array.add(json);
        }
        IMHttpUtil.postString(array.toJSONString()).pushText().enqueue(new HttpIMCallback<MessageHttpBean>() {
            @Override
            protected void onSuccess(MessageHttpBean data) {
                RoomDaoApi.getInstance().saveUserInfo(data.data.users);
                RoomDaoApi.getInstance().saveChatMessage(data.data.messages);
            }

            @Override
            protected void onFail(int code) {

            }
        });
    }

    @Override
    public void update(Observable observable, Object data) {
        if (observable instanceof MessageEvent) {
            if (data instanceof SIMMessage || data == null) {
                SIMMessage msg = (SIMMessage) data;
                if (msg == null) return;
                if (msg.getConversation().getPeer().equals(simConversation.getPeer()) && msg.getConversation().getType() == simConversation.getType()) {
                    presenter.showMessage(msg);
                    //当前聊天界面已读上报，用于多终端登录时未读消息数同步
                    readMessages();
                }
            }
        }
    }

    /**
     * 设置会话为已读
     */
    public void readMessages() {

    }
}
